﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.zyy;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Zyy;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Zyy
{
    public class DeleteReceivingApplicationHandler:IRequestHandler<DeleteReceivingApplicationcommand,ApiResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public DeleteReceivingApplicationHandler(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }
        public async Task<ApiResult<int>> Handle(DeleteReceivingApplicationcommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();

                var del = await db.Queryable<ReceivingApplication>().FirstAsync(p => p.ApplicationId == request.ApplicationId);
                del.IsDelete = true;
                var res = await db.Updateable<ReceivingApplication>(del).ExecuteCommandAsync();
                if (res > 0)
                {
                    result.Code = ApiEnums.删除成功;
                    result.Msg = "删除成功";
                }
                else
                {
                    result.Code = ApiEnums.删除失败;
                    result.Msg = "删除失败";
                }
                return result;
            }
            catch (Exception)
            {

                throw;
            }
            
          
        }
    }
}
